import *as API from '../../api/circle.js'

const state = {
    socialData: [],
    //作者说数据
    authorData: [],
    //书圈数据
    bookCircle: [],
    //关注数据
    attentionData: [],
    lodingAuthorData: [],
    Pages: 3,
    circleData: [],
    //普通用户
    ordinary: []
}

const getters = {  //计算属性

}

const mutations = {  // 同步
    getData(state, data) {
        state.socialData = data.sort((a, b) => { return b.circletime - a.circletime })
    },
    authorTalk(state, data) {
        console.log('获取新数据排序')
        state.authorData = data.filter(item => item.type === '1').sort((a, b) => { return b.circletime - a.circletime });
        state.authorData.map((item, index) => {
            return item.circletime = createTime(item.circletime)
        })
        //转换时间格式
        function createTime(t) {
            let time = null
            let date = new Date(Number(t))
            let year = date.getFullYear(); //获取当前年
            let month = date.getMonth() + 1; //获取当前月 默认0-11
            let day = date.getDate(); //获取当前日
            let hours = date.getHours(); //获取当前小时数(0-23)  
            let minus = date.getMinutes();
            return time = year + "年" + month + "月" + day + "日" + hours + "时" + minus + "分";

        }
    },
    changePages(state, data) {
        state.Pages += data
        state.lodingAuthorData = state.authorData.slice(0, state.Pages)

    },
    setPages(state, data) {
        state.Pages = data

        state.lodingAuthorData = state.authorData.slice(0, state.Pages)
    },
    getCircleData(state, data) {
        state.circleData = data;
    },

    sendPages(state) {
        state.lodingAuthorData = state.authorData.slice(0, state.Pages)
    },
    //普通用户
    ordinary(state, data) {
        state.ordinary = data.filter(item => item.type === '0').sort((a, b) => { return b.circletime - a.circletime });
        state.ordinary.map((item, index) => {
            return item.circletime = createTime(item.circletime)
        })
        //转换时间格式
        function createTime(t) {
            let time = null
            let date = new Date(Number(t))
            let year = date.getFullYear(); //获取当前年
            let month = date.getMonth() + 1; //获取当前月 默认0-11
            let day = date.getDate(); //获取当前日
            let hours = date.getHours(); //获取当前小时数(0-23)  
            let minus = date.getMinutes();
            return time = year + "年" + month + "月" + day + "日" + hours + "时" + minus + "分";

        }
    }
}


const actions = {   // 异步
    // 获取数据
    getData({ commit }, type) {
        return new Promise((resolve, reject) => {
            API.getCircle(type).then(res => {
                // console.log(res)
                if (res.code === 200) {
                    resolve(res)
                        
                    commit('getData', res.info)
                    commit('authorTalk', res.info)
                    commit('setPages', 2)
                    commit('ordinary', res.info)

                }
                        
            })
        })

    },
    // 发布说说
    send({ commit }, data) {
        console.log(data, '发布的数据与类型')
        return new Promise((resolve, reject) => {
            API.send(data.obj).then(res => {
                console.log(res, '返回的类型是否成功')
                if (res.code === 200) {
                    resolve(res)
                    API.getCircle({ type: data.type }).then((res) => {
                        console.log(res)
                        if (data.type === '0') {
                            console.log('是用户发的', res.info)
                            commit('getData', res.info)
                            commit('ordinary', res.info)
                        } else {
                            console.log('是作者发的', res.info)
                            commit('getData', res.info)
                            commit('authorTalk', res.info)
                            commit('sendPages')
                        }
                    })
                } else {
                    reject(res.code)
                }
            })
        })
    },

    getCircleType({ commit }) {
        new Promise((resolve, reject) => {
            API.getCircleType().then((res) => {
                commit('getCircleData', res.info)
            })
        })
    },
    circleContact({ commit }, data) {
        // console.log(data);
        return new Promise((resolve, reject) => {
            API.circleContact(data).then((res) => {
                resolve(res)
            })
        })
    },
}

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}

